package com.whately.core.base.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 手机号码公共处理helper类
 *
 */
public class MobileUtils {

	public static boolean isPhone(String phone) {
		if (phone == null)
			return false;
		//String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
		String regex = "^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";
		if (phone.length() != 11) {
			return false;
		} else {
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(phone);
			boolean isMatch = m.matches();
			return isMatch;
		}
	}

	/**
	 * 11位数字
	 */
	public static boolean isPhoneSimple(String phoneNo){
		if (phoneNo == null)
			return false;

		String regEx = "^(\\d{11})$";
		//String regEx2 = "^[0-9_]+$";//纯数字
		Pattern pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(phoneNo);

		return  matcher.matches();
	}

	/**
	 * 正确的联系方式
	 * 7到20位，数字、+、-
	 */
	public static boolean isCorrectContactInNumbers(String contact) {
		if (contact == null)
			return false;
		String regex = "^[+\\-\\d]{7,20}$";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(contact);
		boolean isMatch = m.matches();
		return isMatch;
	}

	/**
	 *
	 * 方法描述 隐藏手机号中间位置字符，显示前三后三个字符
	 *
	 */
	public static String hidePhoneNo(String phoneNo) {
		if(StringUtils.isBlank(phoneNo)) {
			return phoneNo;
		}

		phoneNo = phoneNo.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");

		return phoneNo;
	}

	/**
	 * 覆盖手机号码中4位为*
	 *
	 * @param mobile
	 * @return
	 */
	public static String coverMobile(String mobile) {
		if (StringUtils.isEmpty(mobile)) {
			return mobile;
		}
		// 11位国内手机号
		if (mobile.matches("\\d{11}")) {
			StringBuffer sb = new StringBuffer();
			sb.append(mobile.substring(0, 3)).append("****").append(mobile.substring(7));
			return sb.toString();
		}

		// 国际号码
		int index = mobile.indexOf("-");
		if (0 < index && mobile.length() > index + 3) {
			StringBuffer sb = new StringBuffer();
			sb.append(mobile.substring(0, index + 4)).append("****");
			if (mobile.length() > index + 7) {
				sb.append(mobile.substring(index + 8));
			}
			return sb.toString();
		}
		return mobile;
	}

	/**
	 * 覆盖手机号码中6位为*
	 *
	 * @param mobile
	 * @return
	 */
	public static String coverMobile2(String mobile) {
		if (StringUtils.isEmpty(mobile)) {
			return mobile;
		}
		// 11位国内手机号
		if (mobile.matches("\\d{11}")) {
			StringBuffer sb = new StringBuffer();
			sb.append(mobile.substring(0, 3)).append("****").append(mobile.substring(7));
			return sb.toString();
		}

		// 国际号码--隐藏中间4位
		int index = mobile.indexOf("-");
		if (0 < index && mobile.length() > index + 3) {
			StringBuffer sb = new StringBuffer();
			sb.append(mobile.substring(0, index + 4)).append("****");
			if (mobile.length() > index + 7) {
				sb.append(mobile.substring(index + 8));
			}
			return sb.toString();
		}
		return mobile;
	}
	public static void main(String[] args) {
		String a = "0531-99999999-1";
		System.out.println(isCorrectContactInNumbers(a));
	}

}
